package com.itfenghuang.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

//阿里云存储工具类
public class OssClient {

        // Endpoint以西南1（成都）为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-chengdu.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tKAZXPfyHXyM5hBchnH";
        String accessKeySecret = "Eb58avi1TSPAyY7HXUiw60X0TwSTac";

    public String uploadfile(String fileName, String bucketName, InputStream inputStream){
        //1.创建OSSClient的实例  地址.账号,密码
        OSS build = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //2.创建PutObjectRequest对象
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
        //3.上传字符串
        PutObjectResult putObjectResult = build.putObject(putObjectRequest);
        //关闭实例
        build.shutdown();
        //返回图片的路径
        return "http://"+bucketName+"."+endpoint+"/"+fileName;

    }
}
